package redis

import (
	"context"
	"sync"

	. "github.com/redis/go-redis/v9"
)

type Cluster struct {
	Cls *ClusterClient
}

func New(c *ClusterClient) *Cluster {
	return &Cluster{c}
}

type Val struct {
	Value string
	sync.Mutex
}

func (c *Cluster) ClientList(ctx context.Context) *Val {

	tmp := &Val{Value: ""}

	err := c.Cls.ForEachMaster(ctx, func(ctx context.Context, master *Client) error {
		str := master.ClientList(ctx).Val()
		tmp.Lock()
		defer tmp.Unlock()
		tmp.Value += str
		return nil
	})
	if err != nil {
		return tmp
	}
	return tmp
}

// 杀掉redis集群上的所有连接
func (c *Cluster) ClientKill(ctx context.Context) (err error) {

	err = c.Cls.ForEachShard(ctx, func(ctx context.Context, client *Client) error {
		var err error
		_, err = client.ClientKillByFilter(ctx, "type", "normal").Result()
		return err
	})

	return err
}
